package zw_301_400.zw_374_猜数字大小;

/**
 * Forward declaration of guess API.
 *
 * @param num your guess
 * @return -1 if num is lower than the guess number
 * 1 if num is higher than the guess number
 * otherwise return 0
 * int guess(int num);
 */

//public class Solution extends GuessGame {
public class Solution {
    /**
     * 双指针
     *
     * @param n
     * @return
     */
    public int guessNumber(int n) {
        int left = 0, right = n;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (guess(mid) == -1) right = mid - 1;
            else if (guess(mid) == 1) left = mid + 1;
            else return mid;
        }
        return left;
    }

    //    public int guessNumber(int n) {
//        int left = 1, right = n;
//        while (left < right) { // 循环直至区间左右端点相同
//            int mid = left + (right - left) / 2; // 防止计算时溢出
//            if (guess(mid) <= 0) {
//                right = mid; // 答案在区间 [left, mid] 中
//            } else {
//                left = mid + 1; // 答案在区间 [mid+1, right] 中
//            }
//        }
//        // 此时有 left == right，区间缩为一个点，即为答案
//        return left;
//    }

    public static int guess(int n) {
        return n;
    }

}